package uk.ac.ebi.pride.peptidome;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

/**
 * PrideMappingReader reads the mappings between peptidome accession and pride accessions
 *
 * User: rwang
 * Date: 03/08/2011
 * Time: 16:10
 */
public class PrideMappingReader {
    private static final Logger logger = LoggerFactory.getLogger(PrideMappingReader.class);

    public Map<String, Map<String, String>> parse() {
        Map<String, Map<String, String>> mappings = new HashMap<String, Map<String, String>>();

        try {
            InputStream inputStream = PrideMappingReader.class.getClassLoader().getResourceAsStream("pride-mapping.tsv");
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            // ignore the first line
            String line = reader.readLine();
            while((line = reader.readLine()) !=  null) {
                String[] parts = line.split("\t");
                if (parts.length == 3) {
                    Map<String, String> samples = mappings.get(parts[0]);
                    if (samples == null) {
                        samples = new HashMap<String, String>();
                        mappings.put(parts[0], samples);
                    }
                    samples.put(parts[1], parts[2]);
                }
            }
        } catch (IOException e) {
            logger.error("Failed to read PRIDE mapping file", e);
        }

        return mappings;
    }
}
